package com.gjh.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.databind.node.DoubleNode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gjh.dao.cargo.ContractDao;
import com.gjh.dao.cargo.ExtCproductDao;
import com.gjh.domain.cargo.Contract;
import com.gjh.domain.cargo.ExtCproduct;
import com.gjh.domain.cargo.ExtCproductExample;
import com.gjh.service.cargo.ContractService;
import com.gjh.service.cargo.ExtCproductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ExtCproductServiceImpl implements ExtCproductService{
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;

    @Override
    public void save(ExtCproduct extCproduct) {
        //计算小计金额
        double amount = extCproduct.getPrice() * extCproduct.getCnumber();
        extCproduct.setAmount(amount);
        //查询合同对象
        String contractId = extCproduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        //修改合同上的附件个数
        contract.setExtNum(contract.getExtNum()+1);
        //修改合同上的总金额
        contract.setTotalAmount(contract.getTotalAmount()+amount);
        contractDao.updateByPrimaryKeySelective(contract);
        extCproductDao.insertSelective(extCproduct);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
        String extCproductId = extCproduct.getId();
        //获取修改之前的附件的对象
        ExtCproduct extCproduct_old = extCproductDao.selectByPrimaryKey(extCproductId);
        Double amount_old = extCproduct_old.getAmount();
        //计算修改后的附件的总金额
        double amount_new = extCproduct.getPrice() * extCproduct.getCnumber();
        extCproduct.setAmount(amount_new);
        extCproductDao.updateByPrimaryKeySelective(extCproduct);

        String contractId = extCproduct_old.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        contract.setTotalAmount(contract.getTotalAmount()-amount_old+amount_new);
        contractDao.updateByPrimaryKeySelective(contract);

    }

    @Override
    public void delete(String id) {
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        Double amount_ext = extCproduct.getAmount();
        String contractId = extCproduct.getContractId();
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        contract.setTotalAmount(contract.getTotalAmount()-amount_ext);
        contract.setExtNum(contract.getExtNum()-1);
        contractDao.updateByPrimaryKeySelective(contract);
        extCproductDao.deleteByPrimaryKey(id);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(example);
        return new PageInfo(extCproducts);
    }
}
